package com.game.plus2048;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import com.game.plus2048.MainActivity.DescendingScoreComparator;
import com.game.plus2048.MainActivity.HScore;
import com.theosirian.twokfortyeight.R;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.KeyListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class NewGameActivity extends Activity {

	private Spinner spinner;
	public String level = null;
	public Button btnGameLevel;
	public Button btnGameType;
	private EditText exponent;
	private CheckBox endless;
	private String scoreKey = "com.theosirian.twokfortyeight.SCOREBOARD_KEY",
			p_name = "";
	private String[] hScoresPrefix = {
			"com.theosirian.twokfortyeight.SCOREBOARD_1",
			"com.theosirian.twokfortyeight.SCOREBOARD_2",
			"com.theosirian.twokfortyeight.SCOREBOARD_3",
			"com.theosirian.twokfortyeight.SCOREBOARD_4",
			"com.theosirian.twokfortyeight.SCOREBOARD_5",
			"com.theosirian.twokfortyeight.SCOREBOARD_6",
			"com.theosirian.twokfortyeight.SCOREBOARD_7",
			"com.theosirian.twokfortyeight.SCOREBOARD_8",
			"com.theosirian.twokfortyeight.SCOREBOARD_9",
			"com.theosirian.twokfortyeight.SCOREBOARD_10" };

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		setContentView(R.layout.activity_new_game);

		btnGameLevel = (Button) findViewById(R.id.rl_type2);
		btnGameLevel.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showlevelgame();
			}
		});

		btnGameType = (Button) findViewById(R.id.rl_type3);
		btnGameType.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				showtypegame();
			}
		});
	}

	public void startNewGame(View v) {

		Intent i = new Intent(NewGameActivity.this, MainActivity.class);
		EditText et = (EditText) findViewById(R.id.et_name);

		final String name = et.getText().toString();

		final int exp;

		String gametype = btnGameType.getText().toString();
		if (gametype == "2048") {
			exp = 11;
		} else {
			if (gametype == "4096") {
				exp = 12;
			} else {
				exp = 13;
			}

		}
		final int gh;
		final int gw;
		String gamelevel = btnGameLevel.getText().toString();
		if (gamelevel == "Easy") {
			gh = 6;
			gw = 6;
		} else {
			if (gamelevel == "Medium") {
				gh = 5;
				gw = 5;
			} else {
				gh = 4;
				gw = 4;
			}

		}
		// final int gh = spinner.getSelectedItemPosition() + 4;
		// final int gw = spinner.getSelectedItemPosition() + 4;

		i.putExtra("player_name", name);
		i.putExtra("win_score", (int) Math.pow(2, exp));
		i.putExtra("grid_height", gh);
		i.putExtra("grid_width", gw);
		startActivity(i);
		// finish();

	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.new_game, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem paramMenuItem) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		switch (paramMenuItem.getItemId()) {
		default:
			return super.onOptionsItemSelected(paramMenuItem);

		case R.id.action_quit:
			showQuitDialog();
			return true;
		case R.id.action_scoreboard:
			Toast.makeText(getApplicationContext(), "ddsd", Toast.LENGTH_LONG).show();
			showScoreboard();
			return true;
		}
	}

	class HScore {
		String name;
		Integer score;

		public HScore(String n, Integer s) {
			this.name = n;
			this.score = s;
		}

		public String getName() {
			return name;
		}

		public Integer getScore() {
			return score;
		}
	}

	class ScoreComparator implements Comparator<HScore> {
		@Override
		public int compare(HScore o1, HScore o2) {
			return o1.getScore().compareTo(o2.getScore());
		}
	}

	class DescendingScoreComparator implements Comparator<HScore> {
		@Override
		public int compare(HScore o1, HScore o2) {
			return o2.getScore().compareTo(o1.getScore());
		}
	}

	private void showlevelgame() {

		final Dialog dialog = new Dialog(NewGameActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(R.layout.dialog_levelgame);

		Button bteasy = (Button) dialog.findViewById(R.id.easy);
		bteasy.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("TEST", "FFFFF");
				btnGameLevel.setText("Easy");
				dialog.dismiss();
			}
		});

		Button btmedium = (Button) dialog.findViewById(R.id.medium);
		btmedium.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("TEST", "FFFFF");
				btnGameLevel.setText("Medium");
				dialog.dismiss();
			}
		});

		Button bthard = (Button) dialog.findViewById(R.id.hard);
		bthard.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("TEST", "FFFFF");
				btnGameLevel.setText("Hard");
				dialog.dismiss();
			}
		});

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
		dialog.show();
		dialog.getWindow().setAttributes(lp);
		// return level;
	}

	private void showtypegame() {

		final Dialog dialog = new Dialog(NewGameActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setCanceledOnTouchOutside(true);
		dialog.setContentView(R.layout.dialog_gametype);

		Button btn2048 = (Button) dialog.findViewById(R.id.type2048);
		btn2048.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Log.d("TEST", "FFFFF");
				btnGameType.setText("2048");
				dialog.dismiss();
			}
		});

		Button btn4096 = (Button) dialog.findViewById(R.id.type4096);
		btn4096.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnGameType.setText("4096");
				dialog.dismiss();
			}
		});

		Button btn8192 = (Button) dialog.findViewById(R.id.type8192);
		btn8192.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				btnGameType.setText("8192");
				dialog.dismiss();
			}
		});

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;
		dialog.show();
		dialog.getWindow().setAttributes(lp);
		// return level;
	}

	private void showScoreboard() {
		final Dialog dialog = new Dialog(NewGameActivity.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
		dialog.setContentView(R.layout.dialog_gridsize);

		final Button btnlevelgame = (Button) dialog
				.findViewById(R.id.btngamelevel);
		btnlevelgame.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				final Dialog dialog3 = new Dialog(NewGameActivity.this);
				dialog3.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog3.setContentView(R.layout.dialog_levelgame);

				Button bteasy3 = (Button) dialog3.findViewById(R.id.easy);
				bteasy3.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.d("TEST", "FFFFF");
						btnlevelgame.setText("Easy");
						dialog3.dismiss();
					}
				});

				Button btmedium3 = (Button) dialog3.findViewById(R.id.medium);
				btmedium3.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.d("TEST", "FFFFF");
						btnlevelgame.setText("Medium");
						dialog3.dismiss();
					}
				});

				Button bthard3 = (Button) dialog3.findViewById(R.id.hard);
				bthard3.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {
						// TODO Auto-generated method stub
						Log.d("TEST", "FFFFF");
						btnlevelgame.setText("Hard");
						dialog3.dismiss();
					}
				});

				WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
				lp.copyFrom(dialog.getWindow().getAttributes());
				lp.width = WindowManager.LayoutParams.WRAP_CONTENT;

				dialog3.show();
				dialog3.getWindow().setAttributes(lp);
			}
		});

		Button button = (Button) dialog.findViewById(R.id.b_gs_show);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {

				SharedPreferences sharedPref = v.getContext()
						.getSharedPreferences(scoreKey, Context.MODE_PRIVATE);

				List<HScore> savedHScores = new ArrayList<HScore>();

				String keyS, keyN;

				String gamelevel = btnlevelgame.getText().toString();

				int height = 0;
				if (gamelevel == "Hard") {
					height = 4;
				}
				if (gamelevel == "Medium") {
					height = 5;
				}
				if (gamelevel == "Easy") {
					height = 6;
				}

				for (int i = 0; i < hScoresPrefix.length; i++) {

					keyS = hScoresPrefix[i] + String.valueOf(height)
							+ String.valueOf(height);
					keyN = hScoresPrefix[i] + String.valueOf(height)
							+ String.valueOf(height) + "N";

					savedHScores.add(new HScore(sharedPref.getString(keyN,
							"------"), sharedPref.getInt(keyS, 0)));

				}

				Collections.sort(savedHScores, new DescendingScoreComparator());

				dialog.dismiss();
				final Dialog dialog2 = new Dialog(NewGameActivity.this);
				dialog2.requestWindowFeature(Window.FEATURE_NO_TITLE);
				dialog2.setContentView(R.layout.dialog_scoreboard);

				TextView tvTitle = (TextView) dialog2
						.findViewById(R.id.tvtitle);
				tvTitle.setText("High Scores (" + String.valueOf(height) + "x"
						+ String.valueOf(height) + ")");
				Button close = (Button) dialog2.findViewById(R.id.b_sb_quit);
				close.setOnClickListener(new View.OnClickListener() {

					@Override
					public void onClick(View v) {

						dialog2.dismiss();

					}

				});

				LinearLayout sbll = (LinearLayout) dialog2
						.findViewById(R.id.sb_ll);

				LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);

				LinearLayout sbRow;
				TextView tv;

				for (int i = 0; i < 10; i++) {

					sbRow = (LinearLayout) inflater.inflate(
							R.layout.scoreboard_row, null);

					tv = (TextView) sbRow.findViewById(R.id.sb_name);
					tv.setText(savedHScores.get(i).getName());

					tv = (TextView) sbRow.findViewById(R.id.sb_score);
					tv.setText(String.valueOf(savedHScores.get(i).getScore()));

					sbll.addView(sbRow);

				}

				WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
				lp.copyFrom(dialog2.getWindow().getAttributes());
				lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
				lp.width = WindowManager.LayoutParams.MATCH_PARENT;

				dialog2.show();

				dialog2.getWindow().setAttributes(lp);

			}

		});

		button = (Button) dialog.findViewById(R.id.b_gs_close);
		button.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				dialog.dismiss();

			}

		});

		WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
		lp.copyFrom(dialog.getWindow().getAttributes());
		lp.width = WindowManager.LayoutParams.WRAP_CONTENT;

		dialog.show();
		dialog.getWindow().setAttributes(lp);

	}

	public void showQuitDialog() {
		new AlertDialog.Builder(this)
				.setTitle("Quit")
				.setMessage("Do you really want to quit?")
				.setPositiveButton("Yes",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface di, int which) {
								finish();
							}
						})
				.setNegativeButton("No", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface di, int which) {
						di.dismiss();
					}
				}).show();
	}

}
